package com.aishua.fire.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aishua.fire.common.IdUtil;
import com.aishua.fire.entity.addressBook.AddressBookInfo;
import com.aishua.fire.entity.beOnDuty.AddBeOnDutyInfo;
import com.aishua.fire.entity.beOnDuty.BeOnDutyInfo;
import com.aishua.fire.entity.beOnDuty.BeOnDutyList;
import com.aishua.fire.entity.beOnDuty.ScheduleAddress;
import com.aishua.fire.mapper.BeOnDutyMapper;
import com.aishua.fire.service.BeOnDutyService;


@Service
public class BeOnDutyServiceImpl implements BeOnDutyService 
{
	
	@Autowired
	private BeOnDutyMapper onMapper;
	
	@Autowired
	private IdUtil idUtil;
	
	/**
	 * 值班管理-值班任务-新增值班任务
	 */
	@Transactional
	public void save(AddBeOnDutyInfo info) 
	{
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		SimpleDateFormat  format = new SimpleDateFormat("yyyyMMdd");
		try 
		{
			Date date = sdf.parse(info.getStartTime());
			String transformDate = format.format(date);
			info.setDate(transformDate);
		} 
		catch (ParseException e) 
		{
			e.printStackTrace();
		}
		if(StringUtils.isBlank(info.getAddressId()))
		{
			ScheduleAddress scheduleAddress = new ScheduleAddress();
			String addressId = idUtil.getAddrId();
			scheduleAddress.setAddressId(addressId);
//			scheduleAddress.setCreateDate(new Date());
			scheduleAddress.setScheduleAddress(info.getScheduleAddress());
			
			onMapper.addScheduleAddress(scheduleAddress);
			
			info.setAddressId(addressId);
		}
		// 添加行程计划接口
		onMapper.addSchedulePlan(info);
	}

	/**
	 *  值班管理-值班任务-查询值班任务列表（按照日期分组
	 * @param info
	 */
	@Override
	public List<BeOnDutyList> selectDutyByDate(String date) 
	{
		List<BeOnDutyList> list = onMapper.selectDutyByDate(date);
		for(BeOnDutyList bean : list)
		{
			String[] titles = bean.getTitle().split(",");
			
			bean.setTitles(titles);
		}
		return list;
	}

	/**
	 * 值班管理-值班任务-查询通讯录下拉框
	 */
	@Override
	public List<AddressBookInfo> selectUserList() 
	{
		return onMapper.selectUserList();
	}

	/**
	 * 工作台 - 获取当前登陆人员的工作台值班任务
	 */
	@Override
	public List<BeOnDutyInfo> selectBeOnDutyListToUser(Long user_id) 
	{
		return onMapper.selectBeOnDutyListToUser(user_id + "");
	}

	/**
	 * 值班管理-值班任务-查询值班地址下拉框
	 */
	@Override
	public List<ScheduleAddress> getAddressSelectList()
	{
		return onMapper.getAddressSelectList();
	}
	
}